package org.hilo.boot.core.shiro;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.SessionFactory;
import org.apache.shiro.subject.support.DefaultSubjectContext;

/**
 * 替换掉默认的SimpleSessionFactory，只为解决频繁的lastAccessTime更改导致也会频繁调用SessionDao update方法
 * 
 * @author zollty
 * @since 2017-07-02
 */
public class ShiroSessionFactory implements SessionFactory {
    
    /**
     * 判断session是否已经成功登录
     */
    public static boolean isLogged(Session session) {
        return !(session.getAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY) == null);
    }

    @Override
    public Session createSession(SessionContext initData) {
        if (initData != null) {
            String host = initData.getHost();
            if (host != null) {
                return new ExtSimpleSession(host);
            }
        }
        return new ExtSimpleSession();
    }

}
